package cn.jcet.hdfs;

import cn.jcet.utils.HDFSUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hdfs.client.HdfsUtils;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

/**
 * @author 鲜磊
 * @Date 2020/11/23 17:11
 * 测试HDFS环境
 */
public class HDFSClient {

    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        System.setProperty("hadoop.home.dir", "D:\\develop\\hadoop-2.7.2");
        System.setProperty("HADOOP_USER_NAME","atguigu");
        //FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.25.102:9000"), conf, "atguigu");
        //设置参数链式额
        conf.set("fs.defaultFS","hdfs://192.168.25.102:9000");
        FileSystem fileSystem = FileSystem.get(conf);
        fileSystem.mkdirs(new Path("/user/atguigu4"));
        fileSystem.close();
        System.out.println("执行结束");
    }


    /**
     * 测试文件上传
     */
    @Test
    public void testCopyFromLocal() throws IOException {
        // 获取连接
        FileSystem fs = HDFSUtils.getConnection();
        /**
         * 测试参数优先级(备份数)
         */
        // 上传文件API put ==> copyFromLocal(源文件，上传远程路径)
        fs.copyFromLocalFile(new Path("d:/course.ftl"),new Path("/user/atguigu2/course2.ftl"));
        // 关闭资源
        fs.close();
        System.out.println("执行结束");
    }

    /**
     * 测试下载为文件
     */
    @Test
    public void testCopyToLocal() throws IOException {
        // 获取连接
        FileSystem fs = HDFSUtils.getConnection();
        // 下载API copyToLocal (源文件，下载路径地址)
        fs.copyToLocalFile(new Path("/user/atguigu/course.ftl"),new Path("d:/cousrse.html"));
        // 关闭资源
        fs.close();
        System.out.println("执行成功");
    }

    /**
     * 测试文件删除
     */
    @Test
    public void testDelete() throws IOException {
        FileSystem connection = HDFSUtils.getConnection();
        //  删除API true上传 false 不删除
        connection.delete(new Path("/user/atguigu4"),true);

        connection.close();
    }

    /**
     * 更改文件名称（重命名）
     */
    @Test
    public void testRename() throws IOException {
        FileSystem connection = HDFSUtils.getConnection();
        // 更改文件或者文件夹名称  重命名
        connection.rename(new Path("/user/atguigu2"),new Path("/user/jcet"));
        connection.close();
    }

    /**
     * 查看文件详情
     */
    @Test
    public void testListFile() throws IOException {
        FileSystem fs = HDFSUtils.getConnection();
        // 文件名称 文件权限 文件长度 文件快
        RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/"), true);
        // 循环迭代器，获取集合信息
        while (iterator.hasNext()){
            LocatedFileStatus status = iterator.next();
            String name = status.getPath().getName();
            FsPermission permission = status.getPermission();
            String group = status.getGroup();
            long len = status.getLen();
            System.out.println("name:" + name);
            System.out.println("permission:" + permission);
            System.out.println("len:" + len);
            System.out.println("group:" + group);
            // blockSize  获取块的信息
            BlockLocation[] blockLocations = status.getBlockLocations();
            // 编辑块信息
            for (BlockLocation blockLocation : blockLocations) {
                // 获取主机
                String[] hosts = blockLocation.getHosts();
                // 循环主机
                for (String host : hosts) {
                    System.out.println("host:" + host);
                }
            }
            System.out.println("------------------");
        }
    }


    /**
     * 文件和文件夹判断
     */
    @Test
    public void testFileType() throws IOException {
        FileSystem fs = HDFSUtils.getConnection();
        // 读取根路径
        RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/"), true);
        while (iterator.hasNext()){
            LocatedFileStatus status = iterator.next();
            // 判断状态是否问文件或者文件夹
            // 如果是文件
            if (status.isFile()){
                System.out.println("f:" + status.getPath().getName());
            }else {
                System.out.println("d:" + status.getPath().getName());
            }
        }
        //  关闭资源
        fs.close();
    }
}
